package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.StrategyContent;
import cn.wolfcode.trip.base.domain.StrategyDetail;
import cn.wolfcode.trip.base.mapper.StrategyContentMapper;
import cn.wolfcode.trip.base.mapper.StrategyDetailMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.TravelQuery;
import cn.wolfcode.trip.base.service.IStrategyDetailService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {
    @Autowired
    private StrategyDetailMapper strategyDetailMapper;
    @Autowired
    private StrategyContentMapper contentMapper;

    @Override
    public PageInfo query(TravelQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<StrategyDetail> list = strategyDetailMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public void saveOrUpdate(StrategyDetail entity) {
        //判断序号是否为null
        if (entity.getSequence() == null) {
            //获取当前的攻略分类中攻略文章最大的序号
            Integer max = strategyDetailMapper.selectMaxSequence(entity.getCatalog().getId());
            entity.setSequence(max + 1);
        }

        //获取攻略内容对象
        StrategyContent strategyContent = entity.getStrategyContent();
        Date date = new Date();//当前时间
        if (entity.getId() != null) {
            //修改
            //查询之前是否为发布状态
            StrategyDetail temp = strategyDetailMapper.selectStateAndReleaseTime(entity.getId());
            //将之前的发布时间设置回去
            entity.setReleaseTime(temp.getReleaseTime());
            //判断当前是否是发布状态，并且之前是草稿状态
            if (entity.getState() == StrategyDetail.STATE_RELEASE && temp.getState() == StrategyDetail.STATE_NOMAL) {
                entity.setReleaseTime(date);
            }
            //判断当前是否为草稿状态，如果是，清除发布时间
            if (entity.getState() == StrategyDetail.STATE_NOMAL) {
                entity.setReleaseTime(null);
            }
            //更新攻略文章
            strategyDetailMapper.updateByPrimaryKey(entity);
            //更新攻略内容表
            strategyContent.setId(entity.getId());
            contentMapper.updateByPrimaryKey(strategyContent);
        } else {
            //新增
            entity.setCreateTime(date);
            //判断是否是发布状态
            if (entity.getState() == StrategyDetail.STATE_RELEASE) {
                entity.setReleaseTime(date);
            }
            //保存攻略文章
            strategyDetailMapper.insert(entity);
            //更新攻略内容表
            strategyContent.setId(entity.getId());
            contentMapper.insert(strategyContent);
        }
    }

    @Override
    public StrategyContent getContentById(Long id) {
        return contentMapper.selectByPrimaryKey(id);
    }

    @Override
    public StrategyDetail getDetailsById(Long id) {
        return strategyDetailMapper.selectByPrimaryKey(id);
    }
}
